/*
 * ChooseGoodsManager.java
 * business
 *
 * Created by ChenTao on 2016/10/20.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.manager;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/** 我要报价，选择商品 */
public class ChooseGoodsManager {

    private List<Goods> chooseGoodsList = new ArrayList<>();

    private ChooseGoodsManager() {
    }

    public static ChooseGoodsManager getInstance() {
        return Singleton.instance;
    }

    /** 初始化选择商品列表 */
    public void initGoodsList(List<Goods> goodsList) {
        chooseGoodsList.clear();
        chooseGoodsList.addAll(goodsList);
    }

    private void clearAllGoodsSelected() {
        for (Goods goods : chooseGoodsList) {
            goods.setSelected(false);
        }
    }

    /** 选择需要提交的商品 */
    public boolean selectGoods(Goods chooseGoods) {

        // 如果当前选择的商品与之前选中的商品不一样，提示只能选择一个商品
        if (getSelectedGoods() != null && !chooseGoods.equals(getSelectedGoods())) {
            UIUtils.showToast(UIUtils.getString(R.string.can_only_choose_one_goods));
            return false;
        }

        // 如果当前选择的商品与之前选中的商品一样，则反选
        if (chooseGoods.equals(getSelectedGoods())) {
            chooseGoods.setSelected(false);
            return false;
        }

        clearAllGoodsSelected();
        chooseGoods.setSelected(true);

        return true;
    }

    /** 获取选择的商品 */
    public Goods getSelectedGoods() {
        for (Goods goods : chooseGoodsList) {
            if (goods.isSelected()) {
                return goods;
            }
        }

        return null;
    }

    private static class Singleton {
        private static final ChooseGoodsManager instance = new ChooseGoodsManager();
    }
}
